﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Windows.Forms;
using DataAccess;

namespace BlueGodWeb
{
    public partial class Home : System.Web.UI.Page
    {
        protected enum SourceType
        {
            kNull,
            kMovie,
            kTV,
            kCartoon,
            kShow
        }
        protected const int kMaxNum = 32;
        protected const string ALL = "All";
        protected static string type = ALL;
        protected static string region = ALL;
        protected static string year = ALL;
        
        protected static List<MOVIE> movie_list = new List<MOVIE>();
        protected static List<TV> tv_list = new List<TV>();
        protected static List<CARTOON> cartoon_list = new List<CARTOON>();
        protected static List<SHOW> show_list = new List<SHOW>();

        protected static SourceType source_type_ = SourceType.kMovie;
        protected void Page_Load(object sender, EventArgs e)
        {
            string select_type = Request.Form["select_type"];
            string msg = Request.Form["message"];
            switch (select_type)
            {
                case "type":
                    type = msg;
                    break;
                case "region":
                    region = msg;
                    break;
                case "year":
                    year = msg.Replace("x", "");
                    break;
                case "search":
                    switch (source_type_)
                    {
                        default:
                        case SourceType.kMovie:
                            movie_list = new DataReader().GetMovies(msg, ALL == type ? null : type, ALL == region ? null : region, ALL == year ? null : year, 0, kMaxNum);
                            break;
                        case SourceType.kTV:
                            tv_list = new DataReader().GetTVs(msg, ALL == type ? null : type, ALL == region ? null : region, ALL == year ? null : year, 0, kMaxNum);
                            break;
                        case SourceType.kCartoon:
                            cartoon_list = new DataReader().GetCartoons(msg, ALL == type ? null : type, ALL == region ? null : region, ALL == year ? null : year, 0, kMaxNum);
                            break;
                        case SourceType.kShow:
                            show_list = new DataReader().GetShows(msg, ALL == type ? null : type, ALL == region ? null : region, ALL == year ? null : year, 0, kMaxNum);
                            break;
                    }
                    break;
                case "more":
                    switch (source_type_)
                    {
                        default:
                        case SourceType.kMovie:
                            movie_list = new DataReader().GetMovies(null, null, null, null, 0, kMaxNum * 20);
                            break;
                        case SourceType.kTV:
                            tv_list = new DataReader().GetTVs(null, null, null, null, 0, kMaxNum * 20);
                            break;
                        case SourceType.kCartoon:
                            cartoon_list = new DataReader().GetCartoons(null, null, null, null, 0, kMaxNum * 20);
                            break;
                        case SourceType.kShow:
                            show_list = new DataReader().GetShows(null, null, null, null, 0, kMaxNum * 20);
                            break;
                    }
                    break;
                default:
                case "movie":
                    source_type_ = SourceType.kMovie;
                    movie_list = new DataReader().GetMovies(null, null, null, null, 0, kMaxNum);
                    break;
                case "tv":
                    source_type_ = SourceType.kTV;
                    tv_list = new DataReader().GetTVs(null, null, null, null, 0, kMaxNum);
                    break;
                case "cartoon":
                    source_type_ = SourceType.kCartoon;
                    cartoon_list = new DataReader().GetCartoons(null, null, null, null, 0, kMaxNum);
                    break;
                case "show":
                    source_type_ = SourceType.kShow;
                    show_list = new DataReader().GetShows(null, null, null, null, 0, kMaxNum);
                    break;
            }

            switch (source_type_)
            {
                default:
                case SourceType.kMovie:
                    DealSelect(movie_list, movie_list.Count <= kMaxNum);
                    break;
                case SourceType.kTV:
                    DealSelect(tv_list, tv_list.Count <= kMaxNum);
                    break;
                case SourceType.kCartoon:
                    DealSelect(cartoon_list, movie_list.Count <= kMaxNum);
                    break;
                case SourceType.kShow:
                    DealSelect(show_list, tv_list.Count <= kMaxNum);
                    break;
            }
        }

        //////////////////////////////////////////////////////////////////////////
        public void DealSelect(List<MOVIE> records, bool with_picture)
        {
            string inner_html = "<div id='test'>";
            inner_html += "<div class='slides_container'>";
            inner_html += "<div class='boxb_2'>";
            for (int j = 0; j < records.Count; j++)
            {
                MOVIE movie = records.ElementAt(j);
                if (with_picture)
                {
                    inner_html += FormPicHtml(movie);
                }
                else
                {
                    inner_html += FormLstHtml(movie);
                }
            }
            inner_html += "</div></div></div>";
            Response.Write(inner_html);
        }

        public string FormPicHtml(MOVIE record)
        {
            int len = record.region.Length;
            int region_line_pos = record.region.IndexOf("/");
            if (region_line_pos > 0)
            {
                len = region_line_pos;
            }
            bool is_today = false;
            string time = DateTime.Now.ToString("yyy-MM-dd");
            if (record.create_time.IndexOf(time) >= 0)
            {
                is_today = true;
            }
            string poster_file_name = null;
            if (record.poster_file_name.Length <= 0)
            {
                poster_file_name = "pic\\default_poster.jpg";
            }
            else
            {
                poster_file_name = "images\\MOVIE\\" + record.poster_file_name;
            }
            string first_showtime = record.first_showtime;
            if (record.first_showtime.Length > 10)
            {
                first_showtime = record.first_showtime.Substring(0, 10);
            }
            string html = " <div class='commonMovieDetail' srcName=\"" + record.name + "\" srcType=\"movie\">"
                           + " <div class='commonPhoto'>"
                           + " <img src='" + poster_file_name + "' class='photoChangeToSmaller'/> </div>"
                           + " <div class='commonInfo'>"
                           + " <table id='smallTable'>"
                           + (is_today ? "<div id='movieNameToday'>" : "<div id='movieName'>") + record.name + "</div>"
                           + "<div>" + first_showtime + "</div>"
                           + "<div>" + record.region.Substring(0, len) + "</div>"
                           + "<div>" + record.type + "</div>"
                           + "</table></div> </div>";
            return html;
        }
        public string FormLstHtml(MOVIE record)
        {
            bool is_today = false;
            string time = DateTime.Now.ToString("yyy-MM-dd");
            if (record.create_time.IndexOf(time) >= 0)
            {
                is_today = true;
            }
            string html = " <div class='commonMovieNormal' srcName=\"" + record.name + "\" srcType=\"movie\">"
                           + (is_today ? "<div id='movieNameToday'>" : "<div id='movieName'>") + "片　　名： " + record.name + "</div>"
                           + "<div>" + "上映时间： " + record.first_showtime + "</div>"
                           + "<div>" + "地　　区： " + record.region + "</div>"
                           + "<div>" + "类　　型： " + record.type + "</div>"
                           + "</div>";
            return html;
        }

        //////////////////////////////////////////////////////////////////////////
        public void DealSelect(List<TV> records, bool with_picture)
        {
            string inner_html = "<div id='test'>";
            inner_html += "<div class='slides_container'>";
            inner_html += "<div class='boxb_2'>";
            for (int j = 0; j < records.Count; j++)
            {
                TV tv = records.ElementAt(j);
                if (with_picture)
                {
                    inner_html += FormPicHtml(tv);
                }
                else
                {
                    inner_html += FormLstHtml(tv);
                }
            }
            inner_html += "</div></div></div>";
            Response.Write(inner_html);
        }

        public string FormPicHtml(TV record)
        {
            int len = record.region.Length;
            int region_line_pos = record.region.IndexOf("/");
            if (region_line_pos > 0)
            {
                len = region_line_pos;
            }
            bool is_today = false;
            string time = DateTime.Now.ToString("yyy-MM-dd");
            if (record.create_time.IndexOf(time) >= 0)
            {
                is_today = true;
            }
            string poster_file_name = null;
            if (record.poster_file_name.Length <= 0)
            {
                poster_file_name = "pic\\default_poster.jpg";
            }
            else
            {
                poster_file_name = "images\\TV\\" + record.poster_file_name;
            }
            string first_showtime = record.first_showtime;
            if (record.first_showtime.Length > 10)
            {
                first_showtime = record.first_showtime.Substring(0, 10);
            }
            string html = " <div class='commonMovieDetail' srcName=\"" + record.name + "\" srcType=\"tv\">"
                           + " <div class='commonPhoto'>"
                           + " <img src='" + poster_file_name + "' class='photoChangeToSmaller'/> </div>"
                           + " <div class='commonInfo'>"
                           + " <table id='smallTable'>"
                           + (is_today ? "<div id='movieNameToday'>" : "<div id='movieName'>") + record.name + "</div>"
                           + "<div>" + first_showtime + "</div>"
                           + "<div>" + record.region.Substring(0, len) + "</div>"
                           + "<div>" + record.type + "</div>"
                           + "</table></div> </div>";
            return html;
        }
        public string FormLstHtml(TV record)
        {
            bool is_today = false;
            string time = DateTime.Now.ToString("yyy-MM-dd");
            if (record.create_time.IndexOf(time) >= 0)
            {
                is_today = true;
            }
            string html = " <div class='commonMovieNormal' srcName=\"" + record.name + "\" srcType=\"tv\">"
                           + (is_today ? "<div id='movieNameToday'>" : "<div id='movieName'>") + "片　　名： " + record.name + "</div>"
                           + "<div>" + "上映时间： " + record.first_showtime + "</div>"
                           + "<div>" + "地　　区： " + record.region + "</div>"
                           + "<div>" + "类　　型： " + record.type + "</div>"
                           + "</div>";
            return html;
        }

        //////////////////////////////////////////////////////////////////////////
        public void DealSelect(List<CARTOON> records, bool with_picture)
        {
            string inner_html = "<div id='test'>";
            inner_html += "<div class='slides_container'>";
            inner_html += "<div class='boxb_2'>";
            for (int j = 0; j < records.Count; j++)
            {
                CARTOON cartoon = records.ElementAt(j);
                if (with_picture)
                {
                    inner_html += FormPicHtml(cartoon);
                }
                else
                {
                    inner_html += FormLstHtml(cartoon);
                }
            }
            inner_html += "</div></div></div>";
            Response.Write(inner_html);
        }

        public string FormPicHtml(CARTOON record)
        {
            int len = record.region.Length;
            int region_line_pos = record.region.IndexOf("/");
            if (region_line_pos > 0)
            {
                len = region_line_pos;
            }
            bool is_today = false;
            string time = DateTime.Now.ToString("yyy-MM-dd");
            if (record.create_time.IndexOf(time) >= 0)
            {
                is_today = true;
            }
            string poster_file_name = null;
            if (record.poster_file_name.Length <= 0)
            {
                poster_file_name = "pic\\default_poster.jpg";
            }
            else
            {
                poster_file_name = "images\\CARTOON\\" + record.poster_file_name;
            }
            string first_showtime = record.first_showtime;
            if (record.first_showtime.Length > 10)
            {
                first_showtime = record.first_showtime.Substring(0, 10);
            }
            string html = " <div class='commonMovieDetail' srcName=\"" + record.name + "\" srcType=\"cartoon\">"
                           + " <div class='commonPhoto'>"
                           + " <img src='" + poster_file_name + "' class='photoChangeToSmaller'/> </div>"
                           + " <div class='commonInfo'>"
                           + " <table id='smallTable'>"
                           + (is_today ? "<div id='movieNameToday'>" : "<div id='movieName'>") + record.name + "</div>"
                           + "<div>" + first_showtime + "</div>"
                           + "<div>" + record.region.Substring(0, len) + "</div>"
                           + "<div>" + record.type + "</div>"
                           + "</table></div> </div>";
            return html;
        }
        public string FormLstHtml(CARTOON record)
        {
            bool is_today = false;
            string time = DateTime.Now.ToString("yyy-MM-dd");
            if (record.create_time.IndexOf(time) >= 0)
            {
                is_today = true;
            }
            string html = " <div class='commonMovieNormal' srcName=\"" + record.name + "\" srcType=\"cartoon\">"
                           + (is_today ? "<div id='movieNameToday'>" : "<div id='movieName'>") + "片　　名： " + record.name + "</div>"
                           + "<div>" + "上映时间： " + record.first_showtime + "</div>"
                           + "<div>" + "地　　区： " + record.region + "</div>"
                           + "<div>" + "类　　型： " + record.type + "</div>"
                           + "</div>";
            return html;
        }

        //////////////////////////////////////////////////////////////////////////
        public void DealSelect(List<SHOW> records, bool with_picture)
        {
            string inner_html = "<div id='test'>";
            inner_html += "<div class='slides_container'>";
            inner_html += "<div class='boxb_2'>";
            for (int j = 0; j < records.Count; j++)
            {
                SHOW show = records.ElementAt(j);
                if (with_picture)
                {
                    inner_html += FormPicHtml(show);
                }
                else
                {
                    inner_html += FormLstHtml(show);
                }
            }
            inner_html += "</div></div></div>";
            Response.Write(inner_html);
        }

        public string FormPicHtml(SHOW record)
        {
            int len = record.region.Length;
            int region_line_pos = record.region.IndexOf("/");
            if (region_line_pos > 0)
            {
                len = region_line_pos;
            }
            bool is_today = false;
            string time = DateTime.Now.ToString("yyy-MM-dd");
            if (record.create_time.IndexOf(time) >= 0)
            {
                is_today = true;
            }
            string poster_file_name = null;
            if (record.poster_file_name.Length <= 0)
            {
                poster_file_name = "pic\\default_poster.jpg";
            }
            else
            {
                poster_file_name = "images\\SHOW\\" + record.poster_file_name;
            }
            string first_showtime = record.first_showtime;
            if (record.first_showtime.Length > 10)
            {
                first_showtime = record.first_showtime.Substring(0, 10);
            }
            string html = " <div class='commonMovieDetail' srcName=\"" + record.name + "\" srcType=\"show\">"
                           + " <div class='commonPhoto'>"
                           + " <img src='" + poster_file_name + "' class='photoChangeToSmaller'/> </div>"
                           + " <div class='commonInfo'>"
                           + " <table id='smallTable'>"
                           + (is_today ? "<div id='movieNameToday'>" : "<div id='movieName'>") + record.name + "</div>"
                           + "<div>" + first_showtime + "</div>"
                           + "<div>" + record.region.Substring(0, len) + "</div>"
                           + "<div>" + record.type + "</div>"
                           + "</table></div> </div>";
            return html;
        }
        public string FormLstHtml(SHOW record)
        {
            bool is_today = false;
            string time = DateTime.Now.ToString("yyy-MM-dd");
            if (record.create_time.IndexOf(time) >= 0)
            {
                is_today = true;
            }
            string html = " <div class='commonMovieNormal' srcName=\"" + record.name + "\" srcType=\"show\">"
                           + (is_today ? "<div id='movieNameToday'>" : "<div id='movieName'>") + "片　　名： " + record.name + "</div>"
                           + "<div>" + "上映时间： " + record.first_showtime + "</div>"
                           + "<div>" + "地　　区： " + record.region + "</div>"
                           + "<div>" + "类　　型： " + record.type + "</div>"
                           + "</div>";
            return html;
        }
    }
}